package org.example;

import java.util.*;

public class TextMap {
    public static void demoHashMap() {
        //定义Map集合
        Map<String, String> map = new HashMap<>();
        //添加集合元素  键值对
        map.put("100", "属性");
        map.put("1000", "数量");
        map.put("10000", "判断");
        //获取map元素对应,没有则null
        System.out.println(map.get("100"));
        //元素个数map.size
        //根据key删除键值对，返回key对应的值
        System.out.println(map.remove("1000"));
        //根据key替换键值对，返回原来的value
        System.out.println(map.replace("100", "偶像"));
        //判断集合中是否有对应的key和value
        System.out.println(map.containsKey("10000"));
        System.out.println(map.containsValue("数量"));


        System.out.println(map);

    }

    public static void demoKey() {
        Map<String, String> map = Map.of("101", "兄弟", "102", "爱吃", "103", "学生", "104", "考虑");
        //键遍历:返回Map所有的键
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println("\" "+ key + "\" : \"" +map.get(key)+"\"");
        }
        //迭代器遍历
        Iterator<String> it = keys.iterator();
        while (it.hasNext()){
            String next=it.next();
            System.out.print("key:"+next);
            System.out.println(",value:"+map.get(next));
        }
    }
    public static void demoValue(){
        Map<String, String> map = Map.of("101", "兄弟", "102", "爱吃", "103", "学生", "104", "考虑");
        //值遍历：values（）返回map中所有的值
        Collection<String> values = map.values();
        for (String string : values) {
            System.out.println("value："+values);
        }
//        Iterator<String> it = values.iterator();
//        while (it.hasNext()){
//            System.out.println("迭代器遍历值"+it.next());
//        }
    }

    //键值对遍历：entrySet返回键值对，对象（Set）
    public static void demoEntry(){
        Map<String, String> map = Map.of("101", "兄弟", "102", "爱吃", "103", "学生", "104", "考虑");
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
//        for (Map.Entry<String, String> entry : entrySet) {
//            System.out.print("获取键"+entry.getKey());
//            System.out.println("===获取值"+entry.getValue());
//        }
        //迭代器
        Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();
        while (iterator.hasNext()){
            Map.Entry<String, String> next = iterator.next(); //next()在一次循环中只能调用一次，否则会出现键值不匹配的情况
            System.out.print("key:"+next.getKey());
            System.out.println("====value:"+next.getValue());
        }
    }

    public static void main(String[] args) {
//        demoHashMap();
//        demoKey();
        demoValue();
//        demoEntry();
    }
}
